<template>
  <div class="container">
    <a-space direction="vertical" style="width: 100%" :size="16">
      <a-card style="width: 100%" title="订单数据">
        <a-form :model="formState">
          <a-row :gutter="24">
            <a-col :span="6">
              <a-form-item label="订单编号">
                <a-input
                  style="width: 100%"
                  v-model:value="formState.orderNo"
                  placeholder="输入订单编号"
                />
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="商品名称">
                <a-input
                  style="width: 100%"
                  v-model:value="formState.goodName"
                  placeholder="输入商品名称"
                />
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="订单状态">
                <a-select
                  ref="select"
                  v-model:value="formState.orderState"
                  style="width: 100%"
                  placeholder="订单状态"
                >
                  <a-select-option :value="0">全部</a-select-option>
                  <a-select-option
                    v-for="item in finanicalStateEnum.data"
                    :key="item.value"
                    :value="item.value"
                    >{{ item.name }}</a-select-option
                  >
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="下单时间">
                <a-range-picker
                  v-model:value="dateTime"
                  @change="handleDateChange"
                />
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>

        <div class="layout-flexSpace">
          <a-space wrap>
            <a-button type="primary" @click="handleSearch">查询</a-button>
            <a-button @click="handleReset">重置</a-button>
          </a-space>
          <a-space wrap>
            <a-button type="primary" @click="handleExport">导出</a-button>
          </a-space>
        </div>
      </a-card>

      <a-card>
        <a-table
          :dataSource="dataSource"
          :columns="orderColumns"
          :pagination="pagination"
          @change="handleChangeSize"
        >
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'orderState'">
              <span>
                {{
                  finanicalStateEnum.getNameByValue(record.orderState)
                }}
              </span>
            </template>
            <template v-else-if="column.key === 'action'">
              <a-space>
                <a-button type="primary" @click="handleSee(record.id)">查看</a-button>
              </a-space>
            </template>
          </template>
        </a-table>
      </a-card>
    </a-space>
    <!-- 订单详情 -->
    <goodsOrderInfo ref="goodsOrderInfoRef"></goodsOrderInfo>
  </div>
</template>
<script setup>
import { ref, onMounted } from "vue";
import { orderColumns } from "./columns.js";
import finanicalStateEnum from "@/enums/finanicalStateEnum";
import { getFinanceOrderList, exportFinanceOrderList } from "@/api/finanical";
import goodsOrderInfo from "../orderManage/goodsOrderModule/goodsOrderInfo.vue";
import { exporeFile } from "@/utils/exporeFile";
const dateTime = ref([null, null]);
const pagination = ref({
  current: 1,
  pageSize: 10,
  defaultPageSize: 10,
  total: 0,
  showSizeChanger: true,
  showTotal: (total) => `共 ${total} 条`,
});
const formState = ref({
  page: pagination.value.current,
  pageSize: pagination.value.pageSize,
  goodName: undefined,
  orderNo: undefined,
  orderState: undefined,
  startTime: undefined,
  endTime: undefined,
});
// 修改时间
const handleDateChange = (date, dateString) => {
  dateTime.value = date;
  formState.value.startTime = dateString[0];
  formState.value.endTime = dateString[1];
};
// 查看订单
const goodsOrderInfoRef = ref(null);
const handleSee = (id) => {
  console.log(id);
  goodsOrderInfoRef.value.seeFunc(id);
};
const dataSource = ref([]);
// 获取列表数据
const getFinanceOrderListFn = async () => {
  const { data } = await getFinanceOrderList(formState.value);
  dataSource.value = data.list;
  pagination.value.total = data.total;
};
function handleSearch() {
  formState.value.page = 1;
  formState.value.pageSize = 10;
  getFinanceOrderListFn();
}
function handleReset() {
  dateTime.value = [null, null]
  formState.value = {
    page: 1,
    pageSize: 10,
    goodName: undefined,
    orderNo: undefined,
    orderState: undefined,
    startTime: undefined,
    endTime: undefined,
  };
  pagination.value.current = 1
  pagination.value.pageSize = 10
  getFinanceOrderListFn();
}
// 导出
const handleExport = async () => {
  const data = await exportFinanceOrderList(formState.value);
  exporeFile("订单数据列表.xls", data);
};
function handleChangeSize(page) {
  formState.value.page = page.current;
  formState.value.pageSize = page.pageSize;
  pagination.value.current = page.current;
  pagination.value.pageSize = page.pageSize;
  getFinanceOrderListFn();
}
onMounted(() => {
  getFinanceOrderListFn();
});
</script>

<style lang="less">
.img-item {
  display: flex;
  flex-direction: column;
  align-items: center;

  .img-label {
    margin-top: 6px;
    color: #9599a5;
    font-size: 12px;
  }
}
</style>
